﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using System.Web.UI.HtmlControls;
using MxWeiXinPF.Common;
using System.Text;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class userratepoints_list : ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info = null;
        public string keywords = string.Empty;
        protected int page;
        protected int pageSize;
        protected int totalCount;
        protected int wid = 0;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("userratepoints_list", MXEnums.ActionEnum.Delete.ToString());
            int num = 0;
            int num2 = 0;
            BLL.user_rate _rate = new BLL.user_rate();
            for (int i = 0; i < this.rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)this.rptList.Items[i].FindControl("hidId")).Value);
                CheckBox box = (CheckBox)this.rptList.Items[i].FindControl("chkId");
                if (box.Checked)
                {
                    if (_rate.AuditExists(id))
                    {
                        num2++;
                    }
                    else if (_rate.Delete(id))
                    {
                        num++;
                    }
                    else
                    {
                        num2++;
                    }
                }
            }
            base.AddAdminLog(MXEnums.ActionEnum.Delete.ToString(), string.Concat(new object[] { "删除积分兑换成功", num, "条，失败", num2, "条" }));
            base.JscriptMsg(string.Concat(new object[] { "删除成功", num, "条，失败", num2, "条！" }), Utils.CombUrlTxt("userratepoints_list.aspx", "keywords={0}", new string[] { this.txtKeywords.Text.Trim() }), "Success");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("userratepoints_list.aspx", "keywords={0}", new string[] { this.keywords }));
        }

        protected string CombSqlTxt(string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and (a.remarks like '%" + _keywords + "%' or b.nick_name like '%" + _keywords + "%')");
            }
            if (this.admin_info == null)
            {
                this.admin_info = base.GetAdminInfo();
            }
            //builder.Append(" and (a.shopid=" + this.admin_info.id + ")");
            return builder.ToString();
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("userratepoints_list_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                this.keywords = MXRequest.GetQueryString("keywords");
                this.admin_info = base.GetAdminInfo();
                this.wid = admin_info.parentid;
                
                this.pageSize = this.GetPageSize(10);
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("userratepoints_list", MXEnums.ActionEnum.View.ToString());
                    this.RptBind("a.id>0" + this.CombSqlTxt(this.keywords), "id desc");
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private void RptBind(string _strWhere, string _orderby)
        {
            this.txtKeywords.Text = this.keywords;
            if (this.admin_info.id ==39)
            {
                this.rptList.DataSource = new BLL.user_rate().AdminGetListScore(this.wid, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            }
            else
            {
                this.rptList.DataSource = new BLL.user_rate().AdminGetListScoreEx(39,this.admin_info.id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            }
            this.rptList.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("userratepoints_list.aspx", "keywords={0}", new string[] { this.txtKeywords.Text.Trim() });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        protected void rptList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int id = int.Parse(e.CommandArgument.ToString());
            BLL.user_rate _rate = new BLL.user_rate();
            if (e.CommandName == "Del")
            {
                if (_rate.AuditExists(id))
                {
                    base.JscriptMsg("已完成兑换的记录不可以删除", "", "Success");
                }
                else if (_rate.AdminDelete(id))
                {
                    this.RptBind("a.id>0" + this.CombSqlTxt(this.keywords), "id desc");
                    base.JscriptMsg("删除成功", "", "Success");
                }
            }
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("userratepoints_list", num.ToString(), 0x3840);
            }
            base.Response.Redirect(Utils.CombUrlTxt("userratepoints_list.aspx", "keywords={0}", new string[] { this.keywords }));
        }

        public string JifenConvert(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return "0.0";
            }
            decimal jf = Convert.ToDecimal(str);
            if (jf < 0)
            {
                return (-jf).ToString();
            }
            return str;
        }
    }
}